package io.adminboot.web;

import io.adminboot.domain.form.PasswordForm;
import io.adminboot.domain.param.JqGridParam;
import io.adminboot.domain.vo.PageVO;
import io.adminboot.entity.SysUser;
import io.adminboot.service.SysUserRoleService;
import io.adminboot.service.SysUserService;
import io.adminboot.system.annotation.AddSysLog;
import io.adminboot.system.validator.Assert;
import io.adminboot.system.validator.ValidatorUtils;
import io.adminboot.system.validator.group.AddGroup;
import io.adminboot.system.validator.group.UpdateGroup;
import io.adminboot.utils.Constant;
import io.adminboot.utils.R;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户
 * 
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
//	@RequiresPermissions("sys:user:list")
	public R list(JqGridParam param){
		//只有超级管理员，才能查看所有管理员列表
		Long createUserId = null;
		if(getUserId() != Constant.SUPER_ADMIN){
			createUserId = getUserId();
		}
		PageVO page = sysUserService.queryPage(param.getPageNum(), param.getPageSize(), null, createUserId);

		return R.ok().put("page", page);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info")
	public R info(){
		return R.ok().put("user", getUser());
	}
	
	/**
	 * 修改登录用户密码
	 */
	@AddSysLog("修改密码")
//	@PostMapping("/password")
	public R password(@RequestBody PasswordForm form){
		Assert.isBlank(form.getNewPassword(), "新密码不为能空");
		
		//sha256加密
		String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
		//sha256加密
		String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
				
		//更新密码
		boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
		if(!flag){
			return R.alert("原密码不正确");
		}
		
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@GetMapping("/info/{userId}")
//	@RequiresPermissions("sys:user:info")
	public R info(@PathVariable("userId") Long userId){
		SysUser user = sysUserService.queryById(userId);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		
		return R.ok().put("user", user);
	}
	
	/**
	 * 保存用户
	 */
	@AddSysLog("保存用户")
	@PostMapping("/save")
//	@RequiresPermissions("sys:user:save")
	public R save(@RequestBody SysUser user){
		ValidatorUtils.validateEntity(user, AddGroup.class);
		
		user.setCreateUserId(getUserId());
		sysUserService.save(user);
		
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@AddSysLog("修改用户")
	@PostMapping("/update")
//	@RequiresPermissions("sys:user:update")
	public R update(@RequestBody SysUser user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);

		user.setCreateUserId(getUserId());
		sysUserService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@AddSysLog("删除用户")
	@PostMapping("/delete")
//	@RequiresPermissions("sys:user:delete")
	public R delete(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			return R.alert("系统管理员不能删除");
		}
		
		if(ArrayUtils.contains(userIds, getUserId())){
			return R.alert("当前用户不能删除");
		}
		
		sysUserService.deleteBatch(userIds);
		
		return R.ok();
	}
}
